package com.qh.servlet;

import com.qh.dao.Jdbc;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;

/**
 * com.qh.servlet
 * 26832
 * yjj
 * &#064;description: 注册servlet
 * &#064;author: lxm
 * RegisterServlet
 * &#064;create: 2024-06-06 20:10
 **/
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        //姓名、性别、年龄、身份证号、手机号、用户名、密码
        User user = new User();
        user.setName(req.getParameter("name"));
        user.setSex(req.getParameter("sex"));
        user.setAge(Integer.parseInt(req.getParameter("age")));
        user.setIdcard(req.getParameter("idcard"));
        user.setPhone(req.getParameter("phone"));
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.sout();
        //接入数据库进行插入数据库操作
        String sql = "INSERT INTO users(uname, sex, age, idcard, phone, urootname, pwd) VALUES ('"
                + user.getName() + "', '"
                + user.getSex() + "', '"
                + user.getAge() + "', '"
                + user.getIdcard() + "', '"
                + user.getPhone() + "', '"
                + user.getUsername() + "', '"
                + user.getPassword() + "')";

        Connection con = null;
        Statement smt = null;
        Jdbc jdbc = new Jdbc();
        try{
            con=jdbc.getConnection();
            smt = con.createStatement();
            int i = smt.executeUpdate(sql);
            if(i==1){
                HttpSession session = req.getSession();
                session.setAttribute("idcard",user.getIdcard());
                resp.sendRedirect("login.html");
            }else{
                resp.sendRedirect("register.html");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                jdbc.close(con,smt,null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        //跳转到登录页面
    }
}
class User{
    //姓名、性别、年龄、身份证号、手机号、用户名、密码
    private String name;
    private String sex;
    private int age;
    private String idcard;
    private String phone;
    private String username;
    private String password;


    public void sout() {
        System.out.println("User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", idcard='" + idcard + '\'' +
                ", phone='" + phone + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}');
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getIdcard() {
        return idcard;
    }

    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
